package com.jiazhong.mingxing.boot.boot11.顺序消息;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;

@Slf4j
public class 顺序消息生产2 {
    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("jiazhong-d");
        producer.setNamesrvAddr("124.220.170.254:9876");
        //producer.setDefaultTopicQueueNums(1); // 设置队列的个数 默认4个
        producer.start();
        for (int i = 1; i < 21; i++) {
            String topic = "TopicC";
            String tags = "shunxu";
            String body = "Hello,Shunxu!index:" + i;
            Message message = new Message(topic, tags, body.getBytes());
            // 发送时就指定了发送到那个队列中
            SendResult sendResult = producer.send(message, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    return list.get(0);
                }
            }, 0);
            log.info("sendResult={}", sendResult);
        }
        producer.shutdown();
    }
}

